<template>
    <div id="label_list">
        <div class="container">
            <div class="handle-box">
                <el-form ref="form" :inline="true" :model="form">
                    <el-form-item>
                        <el-button type="success" @click="handleAdd">添加</el-button>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="handleRefresh">刷新</el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-input
                            clearable
                            @clear="handleSearch"
                            @keyup.native.enter="handleSearch"
                            v-model="query.name"
                            placeholder="标签名"
                            class="handle-input mr10"
                        ></el-input>
                    </el-form-item>
                    <!-- <el-form-item>
                        <el-select
                            @change="handleSearch"
                            placeholder="标签类型"
                            :clearable="true"
                            v-model="query.type"
                            class="handle-select mr10"
                        >
                            <el-option key="0" label="通用标签" :value="0"></el-option>
                            <el-option key="1" label="歌曲标签" :value="1"></el-option>
                            <el-option key="2" label="专辑标签" :value="2"></el-option>
                            <el-option key="3" label="歌手标签" :value="3"></el-option>
                            <el-option key="4" label="歌单标签" :value="4"></el-option>
                        </el-select>
                    </el-form-item> -->
                    <el-form-item>
                        <el-button :disabled="disabled" type="primary" @click="handleSearch">
                            <!-- 查询 -->
                            {{ $t('m.queryBtn') }}
                        </el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column width="auto" label="ID" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.id }}</template>
                </el-table-column>

                <el-table-column width="auto" label="一级标签名称" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.name }}</template>
                </el-table-column>

                <el-table-column width="auto" label="标签描述" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.description }}</template>
                </el-table-column>

                <el-table-column width="auto" label="标签类型" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{
                        scope.row.type == 0
                            ? '通用标签'
                            : scope.row.type == 1
                            ? '歌曲标签'
                            : scope.row.type == 2
                            ? '专辑标签'
                            : scope.row.type == 3
                            ? '歌手标签'
                            : scope.row.type == 4
                            ? '歌单标签'
                            : ''
                    }}</template>
                </el-table-column>

                <el-table-column width="auto" label="下一级标签数量" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.subCount }}</template>
                </el-table-column>

                <el-table-column width="auto" label="创建时间" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.createTime }}</template>
                </el-table-column>

                <el-table-column width="auto" label="管理" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleEdit(scope.$index, scope.row)">
                            <!-- 编辑 -->
                            {{ $t('m.editBtn') }}
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination">
                <el-pagination
                    background
                    layout="total,sizes, prev, pager, next, jumper"
                    :page-sizes="[5, 10, 20, 30]"
                    @size-change="handleSizeChange"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>

            <el-dialog title="添加一级标签" :visible.sync="addVisible" width="30%">
                <el-form ref="form" :model="form" label-width="70px" :rules="rules">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="标签" prop="type">
                        <el-select placeholder="标签类型" :clearable="true" v-model="form.type" style="width:100%">
                            <el-option key="0" label="通用标签" :value="0"></el-option>
                            <el-option key="1" label="歌曲标签" :value="1"></el-option>
                            <el-option key="2" label="专辑标签" :value="2"></el-option>
                            <el-option key="3" label="歌手标签" :value="3"></el-option>
                            <el-option key="4" label="歌单标签" :value="4"></el-option>
                        </el-select>
                    </el-form-item> -->
                    <el-form-item label="描述">
                        <el-input
                            type="textarea"
                            class="w"
                            placeholder="请输入内容"
                            v-model="form.description"
                            maxlength="120"
                            show-word-limit
                            rows="5"
                            @keyup.native.enter="saveAdd"
                        >
                        </el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="addVisible = false">取 消</el-button>
                    <el-button
                        :disabled="
                            checkRequire({
                                name: form.name
                            })
                        "
                        type="primary"
                        @click="saveAdd"
                        >确 定</el-button
                    >
                </span>
            </el-dialog>
        </div>
    </div>
</template>

<script>
  import NProgress from 'nprogress';
  import { debounce, throttle } from '@/utils/utils.js';
  export default {
      name: 'LabelList',
      data() {
          return {
              disabled: false,
              rules: {
                  name: [{ required: true, message: '请填写名称', trigger: 'blur' }]
                  // type: [{ required: true, message: '请选择标签', trigger: 'change' }]
              },
              addVisible: false,
              query: {
                  pageIndex: 1,
                  pageSize: 10,
                  name: null
                  // type: null
              },
              tableData: [],
              pageTotal: 0,
              form: {
                  name: '',
                  description: ''
                  // type: ''
              }
          };
      },
      // activated() {
      //     this.getData();
      // },
      watch: {
          $route: {
              deep: true,
              immediate: true, // 可以省略  created / mounted / activated
              handler(a, b) {
                  // console.log(a, b);
                  if (a.name === 'LabelList') {
                      this.tableData = [];
                      this.getData();
                  }
              }
          }
      },
      methods: {
          async getData() {
              try {
                  NProgress.start();
                  this.disabled = true;
                  let url = '/tag/all';
                  let { data: res } = await this.$http[2].get(url, {
                      params: this.$removeField({
                          page: this.query.pageIndex,
                          size: this.query.pageSize,
                          name: this.query.name
                          // type: this.query.type
                          // lvn: 1
                      })
                  });
                  console.log('标签列表', res);
                  if (res.code != '0000') {
                      throw new Error(res.message);
                  }
                  if (res.data.content && res.data.content.length > 0) {
                      res.data.content.sort((a, b) => {
                          return b.createTime - a.createTime;
                      });

                      // 中间处理
                      res.data.content.map((item) => {
                          item.createTime = this.$dateFormat(item.createTime, '', '-');
                      });
                      // 赋值
                      this.tableData = res.data.content;
                      this.pageTotal = res.data.totalElements;
                  } else {
                      this.tableData = [];
                      this.pageTotal = 0;
                  }
                  NProgress.done();
                  this.disabled = false;
              } catch (err) {
                  console.dir(err);
                  NProgress.done();
                  this.disabled = false;
                  this.$message.error({
                      message: err.message,
                      duration: 1500
                  });
              }
          },
          handleAdd() {
              // this.$router.replace({
              //     path: '/add_1/level1/label'
              // });

              this.addVisible = true;
          },
          checkRequire(obj) {
              return this.$isEmpty(obj);
          },
          async saveAdd() {
              try {
                  let url = '/tag/createTagGroup';
                  let { data: res } = await this.$http[2].get(url, {
                      params: this.$removeField({
                          name: this.form.name,
                          description: this.form.description
                          // type: this.form.type + '' ? this.form.type : null
                      })
                  });
                  console.log(res);
                  this.$message.success({
                      message: res.message,
                      duration: 1500
                  });
                  this.addVisible = false;
                  this.form = {
                      name: '',
                      description: ''
                  };
                  this.getData();
              } catch (err) {
                  console.dir(err);
                  this.$message.error({
                      message: err.message,
                      duration: 1500
                  });
              }
          },
          handleRefresh() {
              // window.location.reload();
              this.getData();
          },
          // handleSearch() {
          //     this.$set(this.query, 'pageIndex', 1);
          //     this.getData();
          // },

          handleSearch: debounce(function () {
              this.$set(this.query, 'pageIndex', 1);
              this.getData();
          }, 300),

          handleEdit(index, row) {
              this.$router.replace({
                  path: '/label/level1/label'
                  // query: {
                  //     id: row.id
                  // }
              });
              localStorage.setItem('label_1_id', row.id);
              localStorage.setItem('label_1_type', row.type);
          },
          // 分页导航
          handlePageChange(val) {
              this.$set(this.query, 'pageIndex', val);
              this.getData();
          },
          handleSizeChange(val) {
              this.$set(this.query, 'pageSize', val);
              this.getData();
          }
      }
  };
</script>

<style lang="scss">
  #label_list {
      .table-header {
          .cell {
              white-space: nowrap !important;
          }
      }
      .handle-box {
          margin-bottom: 20px;
          .el-form {
              display: grid;
              grid-auto-flow: column;
              justify-content: flex-start;
              .el-form-item {
                  width: 95%;
                  .el-form-item__content {
                      .el-select,
                      .handle-input {
                          width: 100%;
                      }
                  }
              }
          }
      }

      .handle-select {
          width: 120px;
      }

      .handle-input {
          width: 120px;
          /* width: 69px;
        height: 28px; */
          display: inline-block;
      }
      .table {
          width: 100%;
          font-size: 14px;
      }
      /* .red {
        color: #ff0000;
    } */
      .mr10 {
          margin-right: 10px;
      }
      .table-td-thumb {
          display: block;
          margin: auto;
          width: 40px;
          height: 40px;
      }
  }
</style>
